//
//  DisplayNoticeView.m
//  OASystem
//
//  Created by macos on 14-4-16.
//  Copyright (c) 2014年 macos. All rights reserved.
//

#import "DisplayNoticeView.h"

@interface DisplayNoticeView ()

@end

@implementation DisplayNoticeView

@synthesize passInfoArr;
@synthesize passAttackInfoArr;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        if(!passInfoArr)
            passInfoArr = [[NSArray alloc] init];
        
        if(!passAttackInfoArr)
            passAttackInfoArr = [[NSArray alloc] init];
    }
    return self;
}

//返回
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)initNavi
{
    //初始化UINavigation
    self.navigationItem.titleView  = [[UIView alloc] initWithFrame:CGRectMake(110, 0, 100, 44)];
    UILabel *titleNaviLabel        = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    titleNaviLabel.text            = @"公告栏";
    titleNaviLabel.font            = HEIGHT_FONT(18.0f);
    titleNaviLabel.textAlignment   = NSTextAlignmentCenter;
    titleNaviLabel.backgroundColor = [UIColor clearColor];
    titleNaviLabel.textColor       = [UIColor whiteColor];
    [self.navigationItem.titleView addSubview:titleNaviLabel];
    
    //添加导航又边用户信息
    //初始化UINavigation左按钮
    UIButton *helloButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 10, 80, 24)];
    [helloButton setTitle:[NSString stringWithFormat:@"你好，%@",USER_NICK_NAME] forState:UIControlStateNormal];
    helloButton.titleLabel.font = HEIGHT_FONT(13.0f);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:helloButton];
    
    //初始化UINavigation左按钮
    UIButton *naviButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 10, 45, 24)];
    [naviButton setTitle:@"返回" forState:UIControlStateNormal];
    naviButton.titleLabel.font = HEIGHT_FONT(13.0f);
    [naviButton setBackgroundImage:[UIImage imageNamed:@"title_back_bg_n.png"] forState:UIControlStateNormal];
    [naviButton setBackgroundImage:[UIImage imageNamed:@"title_back_bg_p.png"] forState:UIControlStateHighlighted];
    [naviButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:naviButton];
}

//初始化web内容
- (void)initContentView
{
    titleLabel.font      = HEIGHT_FONT(16.0f);
    titleLabel.text      = [passInfoArr objectAtIndex:9];
    titleLabel.textColor = NOTICE_CONTENT_WORD_COLOR;
    
    //获取信息内容(作者实践以及阅读次数)
    NSString *timeStr = [passInfoArr objectAtIndex:11];
    if(timeStr.length>10)
            timeStr = [[passInfoArr objectAtIndex:11] substringToIndex:10];
    NSString *infoStr = [NSString  stringWithFormat:@"[发稿人：%@   信息日期：%@    阅读次数：%@]",[passInfoArr objectAtIndex:22],timeStr,[passInfoArr objectAtIndex:20]];
    
    infoLabel.font      = NORMAL_FONT(10.0f);
    infoLabel.text      = infoStr;
    infoLabel.textColor = NOTICE_CONTENT_INFO_COLOR;
    
    //获取html内容进行转换
    contentStr = [self convertHtmlStr:[passInfoArr objectAtIndex:6]];
    
    textShowWebView.opaque          = NO;
    textShowWebView.backgroundColor = [UIColor clearColor];
    textShowWebView.scrollView.showsHorizontalScrollIndicator = NO;
    [textShowWebView loadHTMLString:contentStr baseURL:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.view.backgroundColor = NOTICEBOARD_BACKCOLOR;
    
    [self initNavi];
    [self initContentView];

    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"LOAD" object:nil];
    //获取附件信息
    [[SendHttpOperation shareSendHttpOperation] GetAttachInfo:@"GetAttachInfo.ashx"
                                                   CliengGuid:[passInfoArr objectAtIndex:0]];
    [SendHttpOperation shareSendHttpOperation].getAttackInfoDelegate = self;
}

//获取某个公告的回掉方法
- (void)passGetAttackInfo:(NSDictionary *)attackDic
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"REMOVE" object:nil];
    passAttackInfoArr = [NSArray arrayWithArray:[[[[attackDic objectForKey:@"result"] objectForKey:@"list"] objectForKey:@"data"] objectAtIndex:0]];
    
    //链接标题
    attackTitleLabel.text      = [NSString stringWithFormat:@"%@",[passAttackInfoArr objectAtIndex:2]];
    attackTitleLabel.textColor = NOTICE_CONTENT_BACKCOLOR;
    attackTitleLabel.font      = HEIGHT_FONT(12.0f);
    
    //转换链接大小
    int size                   = [[passAttackInfoArr objectAtIndex:4] intValue]/1000;
    attackSizeLable.text       = [NSString stringWithFormat:@"%dKB",size];
    attackTitleLabel.font      = NORMAL_FONT(13.0f);
    attackView.hidden = NO;
    
    //设置链接按钮的边框与圆角
    //添加点击事件
    attackView.layer.cornerRadius = 6;
    attackView.layer.masksToBounds = YES;
    [attackView.layer setBorderWidth:1];
    [attackView.layer setBorderColor:[[UIColor lightGrayColor] CGColor]];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoAttack)];
    [attackView addGestureRecognizer:tap];
}

//调用浏览器打开附件链接
- (void)gotoAttack
{
    NSString *attackUrl = [passAttackInfoArr lastObject];
    
    if([attackUrl hasPrefix:@"/"]||[attackUrl hasPrefix:@" /"])
        attackUrl = [NSString stringWithFormat:@"%@%@",base_url,attackUrl];
    else
        attackUrl = [NSString stringWithFormat:@"%@/%@",base_url,attackUrl];
    
    attackUrl = [attackUrl  stringByReplacingOccurrencesOfString:@" " withString:@""];
    attackUrl = [attackUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    if(![[UIApplication sharedApplication] openURL:[NSURL URLWithString:attackUrl]])
        [[NSNotificationCenter defaultCenter] postNotificationName:@"TIP" object:@{@"msg":@"获取附件失败"}];
}

//对html设置字体以及大小
- (NSString *)convertHtmlStr:(NSString *)paramSTr
{
    NSString *resultStr = [paramSTr stringByReplacingOccurrencesOfString :@"^^^" withString:@"\r\n"];
    
    NSLog(@"resultStr------->%@",resultStr);
    //建立格式
    resultStr = [resultStr stringByAppendingString:@"</div>"];
    NSString *tempStr = [NSString stringWithFormat:@"<div style=\"font-size:12\" style=\"color:#2c2c2c\">%@",resultStr];
    
//    //获取匹配第一个http的位置
//    NSString *compare = @"http:";
//    NSRange range = [tempStr rangeOfString:compare];
//    int location = range.location;
//
//    //获取第一次出现<也就是url结束的位置
//    NSString *tempstr2 = [tempStr substringFromIndex:location];
//    NSString *compare2 = @"<";
//    NSRange range2 = [tempstr2 rangeOfString:compare2];
//    int location2 = range2.location;
//    
//    //截取url
//    NSString *urlStr = [tempStr substringWithRange:NSMakeRange(location, location2)];
//    urlStr = [NSString stringWithFormat:@"<a href=%@ target=\"_blank\">",urlStr];
//    
//    //拼装url格式
//    NSMutableString *returnStr = [[NSMutableString alloc] initWithString:tempStr];
//    [returnStr insertString:@"</a>" atIndex:location2+location+4];
//    [returnStr insertString:urlStr atIndex:location];
//    
//    NSLog(@"String1:%@",returnStr);
    
    return tempStr;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
